export default {
    name: "shopCart",
    data() {
        return {}
    },
    created() {
        
    },
    methods: {
        setShopCartLocalStorage (item, type) {  //type：其他是新增，1是修改
            let storeArr = [];
            if (this.isArrayFn(item)) {
                storeArr = item;
            } else {
                if (!item) return false;
                let temp = {};
                temp.commodityId = item.commodityId || 0;
                temp.quantity = item.quantity || 0;
                //temp.name = item.name || "";
                //temp.imageUrl = item.imageUrl || "";
                if(temp.commodityId > 0 && temp.quantity > 0) {
                    //先获取之前的本地购物车数据
                    let isExist = false;
                    let cartStore = this.getShopCartLocalStroage();
                    if (cartStore && cartStore.length > 0) {
                        if (cartStore.length <= 100) {
                            storeArr = cartStore.map(r => {
                                if (r.commodityId === temp.commodityId) {
                                    if (type === 1) {
                                        r.quantity = temp.quantity;
                                    } else {
                                        r.quantity = parseInt(r.quantity) + parseInt(temp.quantity);
                                    }
                                    isExist = true;
                                }
                                return r;
                            });
                        } else {
                            this.$dialog.alert({ message: "购物车只能添加最多100条记录!" });
                            return false;
                        }
                    }
                    //说明没有重复添加
                    if (!isExist) {
                        storeArr.push(temp);
                    }
                }
            }
            if (storeArr.length <= 0) return false;
            this.setLocalStorage("shopCart", storeArr);
            this.$store.commit('setShoppingCartCount', {
                count: storeArr.length
            });
            return true;
        },
        syncShopCart (items, callback) {
            let shopCartArr =  [];
            let concatArr = [];
            let cartStore = this.getShopCartLocalStroage();
            /*if (!items || items.length <= 0) {
                if (typeof callback === "function")
                    callback();
                return false;
            }*/
            items = items.map(r => {
                return {
                    commodityId: r.commodityId,
                    quantity: r.quantity
                }
            });
            if (cartStore && cartStore.length > 0) {
                if (items && items.length > 0) {
                    concatArr = cartStore.concat(items);
                    for (let i in concatArr) {
                        let a = concatArr[i];
                        let isExist = false;
                        for (let k in shopCartArr) {
                            let s = shopCartArr[k];
                            if (a.commodityId === s.commodityId) {
                                isExist = true;
                                break;
                            }
                        }
                        if (!isExist) {
                            shopCartArr.push(a);
                        }
                    }
                } else {
                    shopCartArr = cartStore;
                }
            } else {
                shopCartArr = items;
            }
            //执行同步购物车操作
            this.updateShopCart(shopCartArr, callback);
        },
        //同步操作
        updateShopCart(shopCartArr, callback) {
            if (!this.isArrayFn(shopCartArr)) {
                return false;
            }
            this.$axios.put(window.consts.apis.updateShopCart, shopCartArr).then(res => {
                if (res.success) {
                    this.setShopCartLocalStorage(shopCartArr);
                    if (typeof callback === "function")
                        callback();
                } else {
                    if (typeof callback === "function")
                        callback();
                }
            }).catch(e => {
                if (typeof callback === "function")
                    callback();
            })
        },
        getShopCartLocalStroage () {
            return this.getLocalStorage("shopCart");
        },
        deleteShopCartLocalStorage(item) {
            if (!item || !item.commodityId || isNaN(item.commodityId) || item.commodityId <= 0) {
                return false;
            }
            let arr = [];
            let cartStore = this.getShopCartLocalStroage();
            let beforeLength = cartStore.length;
            for (let i in cartStore) {
                if (cartStore[i].commodityId != item.commodityId) {
                    arr.push(cartStore[i]);
                }
            }
            this.setLocalStorage("shopCart", arr);
            this.$store.commit('setShoppingCartCount', {
                count: beforeLength - 1
            });
            return true;
        },
        deleteShopCartLocalStorageArray(items, callback) {
            console.log(items);
            if (!items && items.length <= 0) {
                return false;
            }
            let arr = [];
            let cartStore = this.getShopCartLocalStroage();
            let beforeLength = cartStore.length;
            let delCount = items.length;
            for (var i in cartStore) {
                let isDelete = false;
                for (var k in items) {
                    if (cartStore[i].commodityId === items[k].commodityId) {
                        isDelete = true;
                        break;
                    }
                }
                if (!isDelete) {
                    arr.push(cartStore[i]);
                }
            }
            this.setLocalStorage("shopCart", arr);
            this.$store.commit('setShoppingCartCount', {
                count: beforeLength - delCount
            });
            this.updateShopCart(this.getShopCartLocalStroage(), function() { 
                if (typeof callback === "function") {
                    callback();
                }
            });
        },
        clearShopCartLocalStorage () {
            this.deleteLocalStorage("shopCart");
            this.$store.commit('setShoppingCartCount', {
                count: 0
            });
        }
    }
}

